<?php
/* +----------------------------------------------------------------------
 * 作          者：caijx
 +----------------------------------------------------------------------
 * 创建日期：2021-04-13
 +----------------------------------------------------------------------
 * 文件描述：语音识别
 +----------------------------------------------------------------------
 * 升级记录：
 +----------------------------------------------------------------------
 */
header("Content-Type: text/html; charset=utf-8");
class SpeechRecognitionAction extends CommonAction{
	public function index() {
		//dump(33);
		//exit;
		$formid = $this->_request("formid");
		$this->assign('formid', $formid);
		$this->display();
	}
	public function insert() {
		$formid = $this->_request("formid");
		//dump($formid);
		//$result= $_FILES['voice']['name'];// 接收参数
		$media_model = new MediaModel();
		$operator = $media_model->getUserId();
		//获取文件信息
		if(!empty($_FILES['voice']['name'])) {
			try {
				import("ORG.Net.UploadFile");
				$upload = new UploadFile();// 实例化上传类
				$upload->maxSize = C('Voice_LOGO_MAX_SIZE');
				$upload->savePath = C('Voice_UPLOAD_PATH');
				$upload->allowExts = C('Voice_ALLOWEXTS');
				$upload->saveRule = uniqid; //设置上传文件的保存规则
				$upload->supportMulti = false;
				if(!$upload->upload($upload->savePath)) {// 上传错误提示错误信息
					$this->error('语音文件上传失败：'.$upload->getErrorMsg());
				} else {
					$info = $upload->getUploadFileInfo();
					//将文件保存到数据库
					//判断是否提交重复文件
					$data_rs =  $media_model->where("name='".$info[0]['name']."' and type='".$info[0]['type']."' and size='".$info[0]['size']."' and operator='".$operator."'")->find();
					if(empty($data_rs)){
						$media_data['name'] = $info[0]['name'];
						$media_data['type'] = $info[0]['type'];
						$media_data['path'] = "Uploads/Voice/".$info[0]['savename'];
						$media_data['size'] = $info[0]['size'];
						$media_data['ext'] = $info[0]['extension'];
						$media_data['operator'] = $media_model->getUserId();
						$media_data['update_date'] = $media_model->getDate();
						//dump($media_data);
						if(false == $media_model->add($media_data)) {
							$this->error('未上传语音识别文件');
						}
					}else{
						//删除原来的文件
						if(!empty($data_rs['path'])) {
							$this->delFile($data_rs['path']);
						}
						//更新path
						$data_edit = $media_model->where("id = '".$data_rs['id']."'")->setField(['path'=>substr($info[0]['savepath'].$info[0]['savename'], 2),'update_date'=>$media_model->getDate()]);
					}
					//dump($data_edit);
					//exit;
					$sig = new SliceidgeneratorApiAction();
					$app_id = 'aba87a8f';
					$secret_key = '0fe9845537b69df55660279c283553ed';
					//dump($_SERVER['DOCUMENT_ROOT']);
					//$file_path = $info[0]['savepath'].$info[0]['savename'];
					$file_path = $_SERVER['DOCUMENT_ROOT'].__ROOT__."/Uploads/Voice/".$info[0]['savename'];
					//dump($file_path);
					//echo __FILE__ ;
					
					//exit;
					$api = new SliceidgeneratorAction($file_path,$app_id,$secret_key,$sig);
					$rs = $api->run();
					$rs_data = $media_model->where("name='".$info[0]['name']."' and type='".$info[0]['type']."' and size='".$info[0]['size']."' and operator='".$operator."'")->find();
					//识别结果保存数据库
					if(!empty($data_rs)){
						$media_model->where("id = '".$rs_data['id']."'")->setField(['rs'=>$rs]);
					}else{
						$media_model->where("id = '".$rs_data['id']."'")->setField(['rs'=>$rs]);
					}
					
					$this->success('语音识别成功！', __APP__.'?g=Index&m=SpeechRecognition&a=scan&id='.$rs_data['id']."&formid=".$formid);
					//print_r("转写结果：".$rs);
				}
			} catch(Exception $e) {
				$this->error($e->getMessage());
			}
		}else{
			dump(“未上传语音识别文件”);
		}
	}
	public function scan(){
		$formid = $this->_request("formid");
		$this->assign('formid', $formid);
		$rs_id = $_REQUEST['id'];
		$media_model = new MediaModel();
		$rs = $media_model->where("id='".$rs_id."'")->getField("rs");
		//判断是否存在敏感词
		$rs_msg = set_mgc($rs);
		if(!empty($rs_msg)){
			$this->assign('rs_msg',$rs_msg);
			$this->assign('rs_msg_status','a');
		}else{
			$this->assign('rs_msg','语音合法，继续测评！');
			$this->assign('rs_msg_status','b');
		}
		$this->assign('rs',$rs);
		$id = $_REQUEST['id'];
		$this->assign('id',$id);
		//判断审核状态
		$this->display();
	}
	public function start(){
		$formid = $this->_request("formid");
		$this->assign('formid', $formid);
		$model = new GroupsUserModel();
		$userid = cookie('userid');
		$user = $model->where("id='".$userid."'")->find();
		$this->assign('jumpUrl', __APP__ . '?g=Index&m=Form&a=view&formid=' . $formid . '&ops=save&page=' . $user['page']);
		$this->success('被测评教师选择成功，正在跳转至测评页面！');
	}
	public function yun_wz() {
		$id = $this->_request('id');
		$media_model = new MediaModel();
		$text = $media_model->where("id = '".$id."'")->field("rs")->find();
		$a = new SpeechKeyAction($text['rs']);
		$key_text = $a->xfyun();
		$key_arr = array();
		$key_data = array();
		//dump($key_text);
		foreach($key_text as $key=>$value){
			$key_arr[$key] = "'".$value->word."'";
			$key_data[$key] = "{value:".$value->score.",name:'".$value->word."'}";
			
		}
		//数组转成字符串
		$key_arr_string = implode(",",$key_arr);
		$this->assign('key_arr_string',$key_arr_string);
		$key_data_string = implode(",",$key_data);
		$this->assign('key_data_string',$key_data_string);
		//dump($key_arr_string);
		//dump($key_data_string);
		//exit;
		$this->display();
	}
}
?>